package com.flyco.modules.oms.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.constant.StockConstant;
import com.flyco.common.util.BizUtil;
import com.flyco.common.util.HttpUtil;
import com.flyco.modules.oms.entity.*;
import com.flyco.modules.oms.handle.IThirdOrderHandle;
import com.flyco.modules.oms.service.*;
import com.flyco.modules.oms.vo.OmsOrderPage;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.service.IStmOrderService;
import com.flyco.modules.stm.service.IStmStockService;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import com.flyco.modules.stm.vo.StmStockVO;
import org.apache.shiro.SecurityUtils;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.oms.vo.OmsJddjOrderPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.flyco.common.aspect.annotation.AutoLog;

/**
 * @Description: 京东到家订单表
 * @Author: flyco
 * @Date: 2021-08-24
 * @Version: V1.0
 */
@Api(tags = "京东到家订单表")
@RestController
@RequestMapping("/f/oms/omsJddjOrder")
@Slf4j
public class OmsJddjOrderController {
    @Autowired
    private IOmsJddjOrderService omsJddjOrderService;
    @Autowired
    private IOmsJddjOrderProductService omsJddjOrderProductService;
    @Autowired
    private IOmsJddjOrderInvoiceService omsJddjOrderInvoiceService;
    @Autowired
    private IOmsJddjOrderDiscountService omsJddjOrderDiscountService;
    @Autowired
    private IThirdOrderHandle thirdOrderHandle;
    @Autowired
    private IStmStoreInfoService stmStoreInfoService;
    @Autowired
    private IStmOrderService omsOrderService;
    @Autowired
    private IStmStockService stmStockService;
    @Autowired
    private IOmsJddjOrderBillService omsJddjOrderBillService;
    @Autowired
    private IOmsJddjBalanceBillService omsJddjBalanceBillService;

    /**
     * 分页列表查询
     *
     * @param omsJddjOrder
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "京东到家订单表-分页列表查询")
    @ApiOperation(value = "京东到家订单表-分页列表查询", notes = "京东到家订单表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(OmsJddjOrder omsJddjOrder,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<OmsJddjOrder> queryWrapper = QueryGenerator.initQueryWrapper(omsJddjOrder, req.getParameterMap());
        Page<OmsJddjOrder> page = new Page<OmsJddjOrder>(pageNo, pageSize);
        IPage<OmsJddjOrder> pageList = omsJddjOrderService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "京东到家订单表-同步")
    @ApiOperation(value = "京东到家订单表-同步", notes = "京东到家订单表-同步")
    @GetMapping(value = "/sync")
    public Result<?> sync() {
        try {
            StringBuilder url = new StringBuilder();
            url.append(BizUtil.getApiUrl());
            url.append("f/c/jddj/syncJddjOrder");
            String result = HttpUtil.getRequest(url.toString());
            log.info("result",result);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return Result.ok("同步成功");
    }

    /**
     * 添加
     *
     * @param omsJddjOrderPage
     * @return
     */
    @AutoLog(value = "京东到家订单表-添加")
    @ApiOperation(value = "京东到家订单表-添加", notes = "京东到家订单表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody OmsJddjOrderPage omsJddjOrderPage) {
        OmsJddjOrder omsJddjOrder = new OmsJddjOrder();
        BeanUtils.copyProperties(omsJddjOrderPage, omsJddjOrder);
        omsJddjOrderService.save(omsJddjOrder);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param omsJddjOrderPage
     * @return
     */
    @AutoLog(value = "京东到家订单表-编辑")
    @ApiOperation(value = "京东到家订单表-", notes = "京东到家订单表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody OmsJddjOrderPage omsJddjOrderPage) {
        OmsJddjOrder omsJddjOrder = new OmsJddjOrder();
        BeanUtils.copyProperties(omsJddjOrderPage, omsJddjOrder);
        omsJddjOrderService.updateById(omsJddjOrder);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "京东到家订单表-通过id删除")
    @ApiOperation(value = "京东到家订单表-通过id删除", notes = "京东到家订单表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        omsJddjOrderService.delMain(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "京东到家订单表-批量删除")
    @ApiOperation(value = "京东到家订单表-批量删除", notes = "京东到家订单表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.omsJddjOrderService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "京东到家订单表-通过id查询")
    @ApiOperation(value = "京东到家订单表-通过id查询", notes = "京东到家订单表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        OmsJddjOrder omsJddjOrder = omsJddjOrderService.getById(id);
        return Result.ok(omsJddjOrder);
    }

    //===========================以下是子表信息操作相关API====================================

    /**
     * 通过主表id查询京东到家订单明细
     *
     * @param omsJddjOrderProduct
     * @return
     */
    @AutoLog(value = "京东到家订单明细-通过主表id查询")
    @ApiOperation(value = "京东到家订单明细-通过主表id查询", notes = "京东到家订单明细-通过主表id查询")
    @GetMapping(value = "/listOmsJddjOrderProductByMainId")
    public Result<?> listOmsJddjOrderProductByMainId(OmsJddjOrderProduct omsJddjOrderProduct,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        if (omsJddjOrderProduct.getJddjOrderId() != null) {
            QueryWrapper<OmsJddjOrderProduct> queryWrapper = QueryGenerator.initQueryWrapper(omsJddjOrderProduct, req.getParameterMap());
            Page<OmsJddjOrderProduct> page = new Page<OmsJddjOrderProduct>(pageNo, pageSize);
            IPage<OmsJddjOrderProduct> pageList = omsJddjOrderProductService.page(page, queryWrapper);
            return Result.ok(pageList);
        } else {
            return Result.ok();
        }
    }

    /**
     * 添加京东到家订单明细
     *
     * @param omsJddjOrderProduct
     * @return
     */
    @AutoLog(value = "京东到家订单明细-添加")
    @ApiOperation(value = "京东到家订单明细-添加", notes = "京东到家订单明细-添加")
    @PostMapping(value = "/addOmsJddjOrderProduct")
    public Result<?> addOmsJddjOrderProduct(@RequestBody OmsJddjOrderProduct omsJddjOrderProduct) {
        omsJddjOrderProductService.save(omsJddjOrderProduct);
        return Result.ok("添加京东到家订单明细成功！");
    }

    /**
     * 编辑京东到家订单明细
     *
     * @param omsJddjOrderProduct
     * @return
     */
    @AutoLog(value = "京东到家订单明细-编辑")
    @ApiOperation(value = "京东到家订单明细-编辑", notes = "京东到家订单明细-编辑")
    @PutMapping("/editOmsJddjOrderProduct")
    public Result<?> editOmsJddjOrderProduct(@RequestBody OmsJddjOrderProduct omsJddjOrderProduct) {
        omsJddjOrderProductService.updateById(omsJddjOrderProduct);
        return Result.ok("编辑京东到家订单明细成功！");
    }

    /**
     * 通过id删除京东到家订单明细
     *
     * @param id
     * @return
     */
    @AutoLog(value = "京东到家订单明细-通过id删除")
    @ApiOperation(value = "京东到家订单明细-通过id删除", notes = "京东到家订单明细-通过id删除")
    @DeleteMapping(value = "/deleteOmsJddjOrderProduct")
    public Result<?> deleteOmsJddjOrderProduct(@RequestParam(name = "id", required = true) String id) {
        omsJddjOrderProductService.removeById(id);
        return Result.ok("删除京东到家订单明细成功！");
    }

    /**
     * 批量删除京东到家订单明细
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "京东到家订单明细-批量删除")
    @ApiOperation(value = "京东到家订单明细-批量删除", notes = "京东到家订单明细-批量删除")
    @DeleteMapping(value = "/deleteBatchOmsJddjOrderProduct")
    public Result<?> deleteBatchOmsJddjOrderProduct(@RequestParam(name = "ids", required = true) String ids) {
        if (ids == null || "".equals(ids.trim())) {
            return Result.error("参数不识别！");
        }
        this.omsJddjOrderProductService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");

    }

    /**
     * 通过主表id查询京东到家订单发票
     *
     * @param omsJddjOrderInvoice
     * @return
     */
    @AutoLog(value = "京东到家订单发票-通过主表id查询")
    @ApiOperation(value = "京东到家订单发票-通过主表id查询", notes = "京东到家订单发票-通过主表id查询")
    @GetMapping(value = "/listOmsJddjOrderInvoiceByMainId")
    public Result<?> listOmsJddjOrderInvoiceByMainId(OmsJddjOrderInvoice omsJddjOrderInvoice,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        if (omsJddjOrderInvoice.getJddjOrderId() != null) {
            QueryWrapper<OmsJddjOrderInvoice> queryWrapper = QueryGenerator.initQueryWrapper(omsJddjOrderInvoice, req.getParameterMap());
            Page<OmsJddjOrderInvoice> page = new Page<OmsJddjOrderInvoice>(pageNo, pageSize);
            IPage<OmsJddjOrderInvoice> pageList = omsJddjOrderInvoiceService.page(page, queryWrapper);
            return Result.ok(pageList);
        } else {
            return Result.ok();
        }
    }

    /**
     * 添加京东到家订单发票
     *
     * @param omsJddjOrderInvoice
     * @return
     */
    @AutoLog(value = "京东到家订单发票-添加")
    @ApiOperation(value = "京东到家订单发票-添加", notes = "京东到家订单发票-添加")
    @PostMapping(value = "/addOmsJddjOrderInvoice")
    public Result<?> addOmsJddjOrderInvoice(@RequestBody OmsJddjOrderInvoice omsJddjOrderInvoice) {
        omsJddjOrderInvoiceService.save(omsJddjOrderInvoice);
        return Result.ok("添加京东到家订单发票成功！");
    }

    /**
     * 编辑京东到家订单发票
     *
     * @param omsJddjOrderInvoice
     * @return
     */
    @AutoLog(value = "京东到家订单发票-编辑")
    @ApiOperation(value = "京东到家订单发票-编辑", notes = "京东到家订单发票-编辑")
    @PutMapping("/editOmsJddjOrderInvoice")
    public Result<?> editOmsJddjOrderInvoice(@RequestBody OmsJddjOrderInvoice omsJddjOrderInvoice) {
        omsJddjOrderInvoiceService.updateById(omsJddjOrderInvoice);
        return Result.ok("编辑京东到家订单发票成功！");
    }

    /**
     * 通过id删除京东到家订单发票
     *
     * @param id
     * @return
     */
    @AutoLog(value = "京东到家订单发票-通过id删除")
    @ApiOperation(value = "京东到家订单发票-通过id删除", notes = "京东到家订单发票-通过id删除")
    @DeleteMapping(value = "/deleteOmsJddjOrderInvoice")
    public Result<?> deleteOmsJddjOrderInvoice(@RequestParam(name = "id", required = true) String id) {
        omsJddjOrderInvoiceService.removeById(id);
        return Result.ok("删除京东到家订单发票成功！");
    }

    /**
     * 批量删除京东到家订单发票
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "京东到家订单发票-批量删除")
    @ApiOperation(value = "京东到家订单发票-批量删除", notes = "京东到家订单发票-批量删除")
    @DeleteMapping(value = "/deleteBatchOmsJddjOrderInvoice")
    public Result<?> deleteBatchOmsJddjOrderInvoice(@RequestParam(name = "ids", required = true) String ids) {
        if (ids == null || "".equals(ids.trim())) {
            return Result.error("参数不识别！");
        }
        this.omsJddjOrderInvoiceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");

    }

    /**
     * 通过主表id查询京东到家订单活动
     *
     * @param omsJddjOrderDiscount
     * @return
     */
    @AutoLog(value = "京东到家订单活动-通过主表id查询")
    @ApiOperation(value = "京东到家订单活动-通过主表id查询", notes = "京东到家订单活动-通过主表id查询")
    @GetMapping(value = "/listOmsJddjOrderDiscountByMainId")
    public Result<?> listOmsJddjOrderDiscountByMainId(OmsJddjOrderDiscount omsJddjOrderDiscount,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        if (omsJddjOrderDiscount.getJddjOrderId() != null) {
            QueryWrapper<OmsJddjOrderDiscount> queryWrapper = QueryGenerator.initQueryWrapper(omsJddjOrderDiscount, req.getParameterMap());
            Page<OmsJddjOrderDiscount> page = new Page<OmsJddjOrderDiscount>(pageNo, pageSize);
            IPage<OmsJddjOrderDiscount> pageList = omsJddjOrderDiscountService.page(page, queryWrapper);
            return Result.ok(pageList);
        } else {
            return Result.ok();
        }
    }

    /**
     * 添加京东到家订单活动
     *
     * @param omsJddjOrderDiscount
     * @return
     */
    @AutoLog(value = "京东到家订单活动-添加")
    @ApiOperation(value = "京东到家订单活动-添加", notes = "京东到家订单活动-添加")
    @PostMapping(value = "/addOmsJddjOrderDiscount")
    public Result<?> addOmsJddjOrderDiscount(@RequestBody OmsJddjOrderDiscount omsJddjOrderDiscount) {
        omsJddjOrderDiscountService.save(omsJddjOrderDiscount);
        return Result.ok("添加京东到家订单活动成功！");
    }

    /**
     * 编辑京东到家订单活动
     *
     * @param omsJddjOrderDiscount
     * @return
     */
    @AutoLog(value = "京东到家订单活动-编辑")
    @ApiOperation(value = "京东到家订单活动-编辑", notes = "京东到家订单活动-编辑")
    @PutMapping("/editOmsJddjOrderDiscount")
    public Result<?> editOmsJddjOrderDiscount(@RequestBody OmsJddjOrderDiscount omsJddjOrderDiscount) {
        omsJddjOrderDiscountService.updateById(omsJddjOrderDiscount);
        return Result.ok("编辑京东到家订单活动成功！");
    }

    /**
     * 通过id删除京东到家订单活动
     *
     * @param id
     * @return
     */
    @AutoLog(value = "京东到家订单活动-通过id删除")
    @ApiOperation(value = "京东到家订单活动-通过id删除", notes = "京东到家订单活动-通过id删除")
    @DeleteMapping(value = "/deleteOmsJddjOrderDiscount")
    public Result<?> deleteOmsJddjOrderDiscount(@RequestParam(name = "id", required = true) String id) {
        omsJddjOrderDiscountService.removeById(id);
        return Result.ok("删除京东到家订单活动成功！");
    }

    /**
     * 批量删除京东到家订单活动
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "京东到家订单活动-批量删除")
    @ApiOperation(value = "京东到家订单活动-批量删除", notes = "京东到家订单活动-批量删除")
    @DeleteMapping(value = "/deleteBatchOmsJddjOrderDiscount")
    public Result<?> deleteBatchOmsJddjOrderDiscount(@RequestParam(name = "ids", required = true) String ids) {
        if (ids == null || "".equals(ids.trim())) {
            return Result.error("参数不识别！");
        }
        this.omsJddjOrderDiscountService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");

    }


    /**
     * 通过主表id查询京东到家订单结算明细
     *
     * @param omsJddjOrderBill
     * @return
     */
    @AutoLog(value = "京东到家订单结算明细-通过主表id查询")
    @ApiOperation(value = "京东到家订单结算明细-通过主表id查询", notes = "京东到家订单结算明细-通过主表id查询")
    @GetMapping(value = "/listOmsJddjOrderBillByMainId")
    public Result<?> listOmsJddjOrderBillByMainId(OmsJddjOrderBill omsJddjOrderBill,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        if (omsJddjOrderBill.getJddjOrderId() != null) {
            QueryWrapper<OmsJddjOrderBill> queryWrapper = QueryGenerator.initQueryWrapper(omsJddjOrderBill, req.getParameterMap());
            Page<OmsJddjOrderBill> page = new Page<OmsJddjOrderBill>(pageNo, pageSize);
            IPage<OmsJddjOrderBill> pageList = omsJddjOrderBillService.page(page, queryWrapper);
            return Result.ok(pageList);
        } else {
            return Result.error("请选择京东到家订单");
        }
    }

    /**
     * 通过主表ID查询京东到家对账单
     *
     * @param omsJddjBalanceBill
     * @return
     */
    @AutoLog(value = "京东到家对账单-通过主表id查询")
    @ApiOperation(value = "京东到家对账单-通过主表id查询", notes = "京东到家对账单-通过主表id查询")
    @GetMapping(value = "/listOmsJddjBalanceBillByMainId")
    public Result<?> listOmsJddjBalanceBillByMainId(OmsJddjBalanceBill omsJddjBalanceBill,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        if (oConvertUtils.isNotEmpty(omsJddjBalanceBill.getJddjOrderId())) {
            QueryWrapper<OmsJddjBalanceBill> queryWrapper = QueryGenerator.initQueryWrapper(omsJddjBalanceBill, req.getParameterMap());
            Page<OmsJddjBalanceBill> page = new Page<>(pageNo, pageSize);
            IPage<OmsJddjBalanceBill> pageList = omsJddjBalanceBillService.page(page, queryWrapper);
            return Result.ok(pageList);
        } else {
            return Result.error("请选择京东到家订单");
        }
    }


    /**
     * 导出excel
     *
     * @param request
     * @param omsJddjOrder
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OmsJddjOrder omsJddjOrder) {
        // Step.1 组装查询条件
        QueryWrapper<OmsJddjOrder> queryWrapper = QueryGenerator.initQueryWrapper(omsJddjOrder, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        //Step.2 获取导出数据
        List<OmsJddjOrderPage> pageList = new ArrayList<OmsJddjOrderPage>();
        List<OmsJddjOrder> omsJddjOrderList = omsJddjOrderService.list(queryWrapper);
        for (OmsJddjOrder temp : omsJddjOrderList) {
            OmsJddjOrderPage vo = new OmsJddjOrderPage();
            BeanUtils.copyProperties(temp, vo);
            List<OmsJddjOrderProduct> omsJddjOrderProductList = omsJddjOrderProductService.selectByMainId(temp.getId());
            vo.setOmsJddjOrderProductList(omsJddjOrderProductList);
            List<OmsJddjBalanceBill> omsJddjBalanceBillList = omsJddjBalanceBillService.selectByMainId(temp.getId());
            vo.setOmsJddjBalanceBillList(omsJddjBalanceBillList);
            /*List<OmsJddjOrderInvoice> omsJddjOrderInvoiceList = omsJddjOrderInvoiceService.selectByMainId(temp.getId());
            vo.setOmsJddjOrderInvoiceList(omsJddjOrderInvoiceList);
            List<OmsJddjOrderDiscount> omsJddjOrderDiscountList = omsJddjOrderDiscountService.selectByMainId(temp.getId());
            vo.setOmsJddjOrderDiscountList(omsJddjOrderDiscountList);*/
            pageList.add(vo);
        }
        //Step.3 调用AutoPoi导出Excel
        ModelAndView mv = new ModelAndView(new EntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "京东到家订单");
        mv.addObject(NormalExcelConstants.CLASS, OmsJddjOrderPage.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("京东到家订单表数据", "导出人:" + sysUser.getRealname(), "京东到家订单表"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        StringBuilder errMsg = new StringBuilder("");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<OmsJddjOrderPage> list = ExcelImportUtil.importExcel(file.getInputStream(), OmsJddjOrderPage.class, params);
                Set<Long> shopIds = list.stream().map(r -> Long.valueOf(r.getDeliveryStationNoIsv())).collect(Collectors.toSet());
                List<Long> jddjIds = list.stream().map(r -> r.getOrderId()).collect(Collectors.toList());
                LambdaQueryWrapper<OmsJddjOrder> jddjOrderQueryWrapper = new LambdaQueryWrapper<>();
                jddjOrderQueryWrapper.in(OmsJddjOrder::getOrderId, jddjIds);
                List<OmsJddjOrder> jddjOrders = omsJddjOrderService.list(jddjOrderQueryWrapper);
                if (jddjOrders.size() > 0) {
                    errMsg.append("存在重复导入单据，单号：");
                    List<OmsJddjOrderPage> repeatOrder = new ArrayList<>();
                    jddjOrders.forEach(o -> {
                        list.stream().filter(r -> Objects.equals(r.getOrderId(), o.getOrderId())).forEach(r -> {
                            errMsg.append(r.getOrderId());
                            errMsg.append(",");
                            repeatOrder.add(r);
                        });
                    });
                    if (repeatOrder.size() > 0) {
                        list.removeAll(repeatOrder);
                    }
                }

                LambdaQueryWrapper<StmStoreInfo> storeQueryWrapper = new LambdaQueryWrapper<>();
                storeQueryWrapper.in(StmStoreInfo::getElemeCode, shopIds);
                List<StmStoreInfo> storeInfos = stmStoreInfoService.list(storeQueryWrapper);
                Map<String, StmStoreInfo> storeInfoMap = storeInfos.stream().collect(Collectors.toMap(StmStoreInfo::getElemeCode, Function.identity(), (key1, key2) -> key2));


                for (OmsJddjOrderPage page : list) {
                    page.setId(thirdOrderHandle.generateOrderSn());
                    OmsJddjOrder po = new OmsJddjOrder();
                    BeanUtils.copyProperties(page, po);
                    if (storeInfoMap.containsKey(String.valueOf(po.getDeliveryStationNo()))) {
                        Long storeId = storeInfoMap.get(String.valueOf(po.getDeliveryStationNo())).getStoreId();
                        OmsOrderPage orderPage = thirdOrderHandle.generateStoreOrderWithJddj(page, storeId);
                        OmsOrder omsOrder = new OmsOrder();
                        BeanUtils.copyProperties(orderPage, omsOrder);
                        omsOrderService.saveMain(omsOrder, orderPage.getOmsOrderItemList(), orderPage.getOmsOrderInvoiceList());
                        omsJddjOrderService.saveMain(po, page.getOmsJddjOrderProductList(), page.getOmsJddjOrderInvoiceList(), page.getOmsJddjOrderDiscountList());
                        List<StmStockVO> stockVOS = thirdOrderHandle.generateStockRecord(orderPage);
                        String changeReason = "京东到家销售出库";
                        stmStockService.stockInOut(stockVOS, storeId, StockConstant.STOCK_OUT, StockConstant.STOCK_OUT_TYPE_SALE, StockConstant.RECEIPT_TYPE_SALE, page.getId(), changeReason);
                    }
                }
                if (list.size() > 0)
                    errMsg.append("文件导入成功！数据行数:" + list.size());
                return Result.ok(errMsg.toString());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        errMsg.append("文件导入失败！");
        return Result.ok(errMsg.toString());
    }
}
